package com.moter.demo01.juc.jmm;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 验证不保证原子性
 */
public class VolatileDemo2 {
    //加不加volatile都可能会导致多个线程重复写入
    private static AtomicInteger num =new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                for (int j = 0; j < 1000; j++) {
                    num.addAndGet(  1);
                }
                System.out.println(num);
            }).start();
        }

        TimeUnit.SECONDS.sleep(2);
        System.out.println("num = " + num);
    }
}
